package edu.gyc.hilover.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.gyc.hilover.dao.StuDao;
import edu.gyc.hilover.model.Info;
import edu.gyc.hilover.model.Stu;
import edu.gyc.hilover.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.context.request.WebRequest;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ls
 * @since 2020-11-27
 */
@Controller
@RequestMapping("/stu")
@SessionAttributes("queryCondition")
public class StuController {

    @Autowired
    private StuService stuService;

    @Autowired
    private StuDao stuDao;

//    //只需要加上下面这段即可，注意不能忘记注解
//    @InitBinder
//    public void initBinder(WebDataBinder binder, WebRequest request) {
//
//        //转换日期
//        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
//        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));// CustomDateEditor为自定义日期编辑器
//    }


//    @RequestMapping({"/list"})
//    public String list(Model model) {
//        List<Stu> stus = stuService.list();
//        model.addAttribute("stus", stus);
//        return "index";
//    }
@RequestMapping("/")
public String list() {

    return "list";
}
    @RequestMapping("/search")
    public String query() {

        return "search";
    }

    @RequestMapping({"/list"})
    public String listByPage(@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum,
                             Model model) {
        PageHelper.startPage(pageNum, 3);
        List<Stu> stus = stuService.lambdaQuery().orderByDesc(Stu::getId).list();
        PageInfo<Stu> pageInfo = new PageInfo<>(stus);

        model.addAttribute("stus", pageInfo);
        return "list";
    }

    @RequestMapping({"/query"})
    public String queryByPage(@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum,
                              Model model, String type, String value) {
        PageHelper.startPage(pageNum, 3);

        if (type != null && value != null) {
            //清除session里的查询条件
            model.addAttribute("queryCondition", null);
        }
        Stu queryStu = (Stu) model.getAttribute("queryCondition");
        Stu stu=new Stu();
        if (queryStu == null) {
            if (value != null && !"".equals(value)) {
                if ("name".equals(type)) {
                    stu.setName(value);
                } else if ("id".equals(type)) {
                    if ("id".equals(type)) {
                        try {
                            Integer id = Integer.valueOf(value);
                            stu.setId(id);

                        } catch (Exception e) {
                        }
                    }
                } else if ("gender".equals(type)) {
                    stu.setGender(value);
                } else if ("birthday".equals(type)) {
                    //stu.setBirthday(value);
                }else if ("keyWords".equals(type)) {
                    stu.setKeyWords(value);
                }
                //将查询条件存储在session中，供分页查询用
                model.addAttribute("queryCondition", stu);
            }
        }else {
            stu=queryStu;
        }



        List<Stu> stus = stuDao.query(stu);
        PageInfo<Stu> pageInfo = new PageInfo<>(stus);
        model.addAttribute("stus", pageInfo);

        return "search";
    }

    @PostMapping("/add")
    public String add(Stu stu, Model model) {
        Info info = new Info();
        if (stuService.save(stu)) {
            info.setSuccess(true);
            info.setMsg("爱妃：" + stu.getName() + "添加成功");
        } else {
            info.setSuccess(false);
            info.setMsg("爱妃：" + stu.getName() + "添加失败");
        }
        model.addAttribute("info", info);
        return "list";
    }

    @PostMapping("/update")
    public String update(Stu stu, Model model) {
        Info info = new Info();
//        if (stuService.lambdaUpdate().eq(Stu::getId,stu.getId()).update()) {
//        if (stuService.update(stu, new UpdateWrapper<Stu>().eq("id", stu.getId()))) {
        if (stuDao.updateById(stu)==1) {
            info.setSuccess(true);
            info.setMsg("爱妃：" + stu.getId() + "修改成功");
        } else {
            info.setSuccess(false);
            info.setMsg("爱妃：" + stu.getId() + "修改失败");
        }
        model.addAttribute("info", info);

        return "list";
    }

    @RequestMapping("/del")
    public String del(Integer id, Model model) {
        Info info = new Info();
//        if (stuService.lambdaUpdate().eq(Stu::getId,stu.getId()).update()) {
        if (stuService.removeById(id)) {
            info.setSuccess(true);
            info.setMsg("爱妃：" + id + "删除成功");
        } else {
            info.setSuccess(false);
            info.setMsg("爱妃：" + id + "删除失败");
        }
        model.addAttribute("info", info);

        return "list";
    }
}

